<?php


namespace app\controller\admin;


use app\common\model\PayOrderModel;
use app\model\AdminPayOrder;
use app\model\AdminSchool;
use app\model\AdminStaff;
use think\facade\Log;
use think\Response;

class PayOrder extends Base
{

    /**
     * @notes: 支付订单信息列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author: speed
     */
    public function index(): Response {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $school_name = $this->request->get('school_name', '');
        $name = $this->request->get('name', '');
        $phone = $this->request->get('phone', '');
        $order_status = $this->request->get('order_status', '');
        $order_no = $this->request->get('order_no', '');
        $device_no = $this->request->get('device_no', '');

        $obj = (new AdminPayOrder());

        $where = [];
        if ($school_name) {
            $school_id_arr = AdminSchool::where('is_delete', 0)
                ->where('name','like', "%{$school_name}%")
                ->column('id');
            array_push($where, ['school_id', 'in', $school_id_arr]);
        }
        if ($name) {
            array_push($where, ['name', 'like', "%{$name}%"]);
        }
        if ($phone) {
            array_push($where, ['phone', 'like', "%{$phone}%"]);
        }
        if ($order_status) {
            $obj = $obj->where('order_status', '=', $order_status);
        }
        if ($order_no) {
            $obj = $obj->where('order_no', 'like', "%{$order_no}%");
        }
        if ($device_no) {
            $obj = $obj->where('device_no', 'like', "%{$device_no}%");
        }
        $listObj = $obj->hasWhere('staff', $where)->with(['staff','school'])
            ->append(['pay_type_text','order_status_text','order_type_text'])
            ->order(['create_time' => 'DESC'])
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        $listInfo = $listObj['data'];
        foreach ($listInfo as $key => &$value) {
            $value['money'] = $value['money'] / 100;
        }

        return $this->buildSuccess([
            'list'  => $listInfo,
            'count' => $listObj['total']
        ]);
    }

    /**
     * 支付回调通知
     * PayOrder/TLPayNotify
     */
    public function TLPayNotify(): ?Response {
        Log::error('支付回调通知');
        $params = array();
        foreach($_POST as $key=>$val) { // 动态遍历获取所有收到的参数,此步非常关键,因为收银宝以后可能会加字段,动态获取可以兼容由于收银宝加字段而引起的签名异常
            $params[$key] = $val;
        }
        if(count($params)<1){//如果参数为空,则不进行处理
            Log::error("参数为空");
            return null;
        }
        Log::error($params);
        $order_no = $params['cusorderid'];
        Log::error('支付回调通知开始处理订单号->'.$order_no);
        $payOrder = AdminPayOrder::where('order_no', $order_no)->find();
        if (!$payOrder) {
            Log::error('支付回调通知处理订单号->'.$order_no.'失败，订单号不存在');
            return null;
        }
        if ($payOrder->order_status != 0){
            Log::error('支付回调通知处理订单号->'.$order_no.'已处理，无需重复处理');
            echo 'success'; // 无需回调
            return null;
        }
        $staff = AdminStaff::where('id', $payOrder->staff_id)->find();
        if (!$staff) {
            Log::error('支付回调通知处理订单号->'.$order_no.'失败，账号不存在或未审核通过');
            return null;
        }
//        if(TLPAYUtil::ValidSign($params, $merchantNet->public_key)){ // 验签成功
//            Log::error("参数验签成功");
//        }
//        else {
//            Log::error("验签失败");
//            return null;
//        }
        $trxstatus = $params['trxstatus'];
        $chnltrxid = $params['chnltrxid'];
        // 支付成功
        if($trxstatus == '0000') {
            PayOrderModel::waterElecCharge($payOrder,$staff);
            Log::error('支付回调通知处理订单号->'.$order_no.'处理成功');
            echo 'success';
        }
        return null;
    }

}